<?php

namespace App\Admin\Service\Traits;


use App\Common\Exception\ApiMessageException;
use App\Common\Util\ImitateAop\Attributes\Transaction;

/**
 * 通用删除操作
 *
 * @method beforeDestroy(array $ids)
 * @method afterDestroy(array $ids)
 */
trait DestroyServiceTrait
{
    /**
     * @param array $ids
     *
     * @return void
     * @throws ApiMessageException
     */
    #[Transaction]
    public function destroy(array $ids): void
    {
        method_exists($this, 'beforeDestroy') and $this->beforeDestroy($ids);

        $result = $this->model->whereIn('id', $ids)->update([
            $this->model->getDeleteTimeField() => time(),
            $this->model->getUpdateTimeField() => date('Y-m-d H:i:s')
        ]);

        if ($result) {
            method_exists($this, 'afterDestroy') and $this->afterDestroy($ids);
        }else {
            throw new ApiMessageException("删除失败！");
        }
    }
}
